#!/usr/bin/python3
import shutil
import subprocess
import hashlib
import sys
from tempfile import mkdtemp


def md5(fname):
    hash_md5 = hashlib.md5()
    with open(fname, "rb") as f:
        for chunk in iter(lambda: f.read(4096), b""):
            hash_md5.update(chunk)
    return hash_md5.hexdigest()


def run_test(cmd, md5sum):
    argv = cmd.split()
    for param in argv:
        if '.ini' in param:
            filename = param
    if filename == 'temp.ini':
        subprocess.run('rm -rf temp.ini', shell=True)
    global fail_cnt
    global testnum
    ret = subprocess.run(cmd, shell=True).returncode
    fail = False
    if md5(filename) == md5sum:
        print(f'Test {testnum} ok')
    else:
        print(f"Test {testnum} failed")
        fail = True
        # print(filename, md5(filename), md5sum)
    if ret or fail:
        fail_cnt += 1
    testnum += 1


def get_test(cmd, ans):
    global fail_cnt
    global testnum
    ret = subprocess.run(cmd, shell=True, capture_output=True).stdout.decode()
    if ret.strip() != ans.strip():
        fail_cnt += 1
        print(f"Test {testnum} failed")
    else:
        print(f'Test {testnum} ok')
    testnum += 1


def retcode_test(cmd, code):
    global fail_cnt
    global testnum
    ret = subprocess.run(cmd, shell=True, capture_output=True).returncode
    if ret != code:
        fail_cnt += 1
        print(f"Test {testnum} failed")
    else:
        print(f'Test {testnum} ok')
    testnum += 1


tempdir = mkdtemp()
shutil.copyfile('section_source.ini', f'{tempdir}/section.ini')
shutil.copyfile('nosection_source.ini', f'{tempdir}/nosection.ini')
shutil.copyfile('indent_source.ini', f'{tempdir}/indent.ini')
fail_cnt = 0
testnum = 1

# general
run_test(f'../py-ini-config.py set {tempdir}/section.ini t s 12', '1876c77f977616cc693708233de67199')
run_test(f'../py-ini-config.py set {tempdir}/section.ini g w 14 -s "="', '9948d2b5c70f256d047d7da29fbcf06f')
run_test(f'../py-ini-config.py set {tempdir}/section.ini a b c -s "="', '2796bfc3eb01fe33d85acfd9a4764b24')
run_test(f'../py-ini-config.py del {tempdir}/section.ini g w', 'ddd14fe8c63d765abc4ccd5c73ca1d1d')
run_test(f'../py-ini-config.py del {tempdir}/section.ini a b --flush', '1876c77f977616cc693708233de67199')
run_test(f'../py-ini-config.py del_section {tempdir}/section.ini g', 'bf0075cbf61fb801342b595ae54c19b6')
run_test(f'../py-ini-config.py del_section {tempdir}/section.ini ew', 'aaa33e99009c875066b298c759b8d615')
run_test(f'../py-ini-config.py comment {tempdir}/section.ini t s', 'e59bb48f1dca5226cbf0d999c71f105f')
run_test(f'../py-ini-config.py uncomment {tempdir}/section.ini t s', 'aaa33e99009c875066b298c759b8d615')
run_test(f'../py-ini-config.py comment {tempdir}/section.ini t', '307632035979fbba84a7572650bf64de')
run_test(f'../py-ini-config.py uncomment {tempdir}/section.ini t fg', '013977e480f4a69c9cf921290a8933b7')
run_test(f'../py-ini-config.py set {tempdir}/section.ini d t 16', 'a214f347a2509b6fea5104fdf186ed47')
run_test(f'../py-ini-config.py set {tempdir}/section.ini d t 84 --view-comments', '0a0fedaf9d22d79e78e217dc63d16025')
run_test(f'../py-ini-config.py set {tempdir}/section.ini d t 9 --view-comments', '97e9362f74d6cc7e52163ef180e2cd6a')
run_test(f'../py-ini-config.py set {tempdir}/section.ini t s 10 --view-comments', '0ad345212b5bfe50c25a507924f117bb')
run_test(f'../py-ini-config.py set {tempdir}/section.ini t s 12 --view-comments', '9aa4cfb98ae201b2ae09428015e281fb')
get_test(f'../py-ini-config.py get {tempdir}/section.ini d t', '9')
get_test(f'../py-ini-config.py get {tempdir}/section.ini t fg', 'fg')
get_test(f'../py-ini-config.py get_commented {tempdir}/section.ini t s', '10')
get_test(f'../py-ini-config.py get_commented {tempdir}/section.ini d t', '16\n84')
run_test(f'../py-ini-config.py set {tempdir}/section.ini a b c -s "="', '6801a10179ccc4b167c72a3f40ec3be9')
run_test(f'../py-ini-config.py del {tempdir}/section.ini a b', 'cc1ae1c2918ca89119722a8b56aaaff0')
retcode_test(f'../py-ini-config.py is_empty {tempdir}/section.ini a', 0)
retcode_test(f'../py-ini-config.py is_empty {tempdir}/section.ini d', 1)
retcode_test(f'../py-ini-config.py is_empty {tempdir}/section.ini t', 1)
run_test(f'../py-ini-config.py comment {tempdir}/section.ini d t', '3b70130cd029607a7082a97e732933a5')
retcode_test(f'../py-ini-config.py is_empty {tempdir}/section.ini d', 1)
retcode_test(f'../py-ini-config.py is_set {tempdir}/section.ini d t', 1)
retcode_test(f'../py-ini-config.py is_set {tempdir}/section.ini t fg', 1)  # not set - no value
retcode_test(f'../py-ini-config.py is_set {tempdir}/section.ini t s', 0)
retcode_test(f'../py-ini-config.py is_set {tempdir}/section.ini a r', 1)
retcode_test(f'../py-ini-config.py is_set {tempdir}/section.ini not_a_section none', 1)
# create
run_test(f'../py-ini-config.py set -c {tempdir}/temp.ini section var value -s " : "', '84bd3323fb8976d675869018e1c6d3b4')
run_test(f'../py-ini-config.py set -c {tempdir}/temp2.ini section var value', 'a0fd466bfac749f2d715dc7d0ff55a52')
# indents, semicolon-comment
get_test(f'../py-ini-config.py get {tempdir}/indent.ini a b -s ":"', 'c')
get_test(f'../py-ini-config.py get {tempdir}/indent.ini b b -s ":"', 'c')
run_test(f'../py-ini-config.py set {tempdir}/indent.ini a b z -s ":"', 'd46a5a9c46bde5d623b837a452e6956f')
get_test(f'../py-ini-config.py get {tempdir}/indent.ini a b -s ":"', 'z')
run_test(f'../py-ini-config.py set {tempdir}/indent.ini a e y -s " : "', '51b31083760b6c377181bad932e89c67')
get_test(f'../py-ini-config.py get {tempdir}/indent.ini a e -s ":"', 'y')
run_test(f'../py-ini-config.py set {tempdir}/indent.ini a g x -s " : " --no-indent', '18efa3705a175350a7a05dc2a1406af7')
get_test(f'../py-ini-config.py get {tempdir}/indent.ini a g -s ":"', 'x')
run_test(f'../py-ini-config.py comment {tempdir}/indent.ini b c -s ":" --semicolon-comment', '118451b8dfde4ba1974e0cdcea362f23')
get_test(f'../py-ini-config.py get_commented {tempdir}/indent.ini b c -s ":" --semicolon-comment', 'd')
run_test(f'../py-ini-config.py uncomment {tempdir}/indent.ini b c -s ":" --semicolon-comment', '18efa3705a175350a7a05dc2a1406af7')
run_test(f'../py-ini-config.py comment {tempdir}/indent.ini a -s ":" --semicolon-comment', 'd220b22035b211b67a04bedd8dd974dd')
get_test(f'../py-ini-config.py get_commented {tempdir}/indent.ini a d -s ":" --semicolon-comment', 'e')
run_test(f'../py-ini-config.py uncomment {tempdir}/indent.ini a d -s ":" --semicolon-comment', 'e906fa14dcbc14089211f6ec1fb7edaf')
run_test(f'../py-ini-config.py set {tempdir}/indent.ini a e w -s ":"', 'a4b479352ae8b1668df7e28809b8270d')
run_test(f'../py-ini-config.py set {tempdir}/indent.ini a c v -s ":" --view-comments --semicolon-comment',
         'f2868b0fd0f61fee986af53044b3115c')
run_test(f'../py-ini-config.py set {tempdir}/indent.ini a c d -s ":" --view-comments --semicolon-comment',
         '9f65f2afd0b907ecde6ba90b0f0a816e')
run_test(f'../py-ini-config.py set {tempdir}/indent.ini a e y -s "  :  " --view-comments --semicolon-comment --no-indent',
         '6f207c4c877b26ff54bd6c8ce923fda3')
run_test(f'../py-ini-config.py comment {tempdir}/indent.ini b -s ":" --semicolon-comment', 'fd113ae6213690667e15aa8736a87467')
run_test(f'../py-ini-config.py set {tempdir}/indent.ini a b z -s ":" --view-comments --semicolon-comment --no-indent',
         'c220475ab6a82d850a4ba71d685e691e')
retcode_test(f'../py-ini-config.py is_set {tempdir}/indent.ini -s ":" a c', 0)
retcode_test(f'../py-ini-config.py is_set {tempdir}/indent.ini -s ":" a f', 1)
run_test(f'../py-ini-config.py set {tempdir}/indent.ini b c z -s "  :  " --view-comments --semicolon-comment --no-indent',
         'b9971b0756ada7b915c16d51451ad990')
run_test(f'../py-ini-config.py set {tempdir}/indent.ini b c d -s "  :  " --view-comments --semicolon-comment',
         '8d65a1e672f0120aef135cfe26c986f9')
# nosection
get_test(f'../py-ini-config.py get {tempdir}/nosection.ini q --no-section', '1')
get_test(f'../py-ini-config.py get {tempdir}/nosection.ini g --no-section', '14')
run_test(f'../py-ini-config.py set {tempdir}/nosection.ini q 15 --no-section', '1fbdf2c2dafbda2483bc715d477c7590')
run_test(f'../py-ini-config.py set {tempdir}/nosection.ini w 16 --no-section', '7fa6c288148f9b0db82d5e8e580c21c5')
run_test(f'../py-ini-config.py set {tempdir}/nosection.ini t 17 --no-section --no-indent', '4a97e22aafbee934ff5c9fbccd81be90')
run_test(f'../py-ini-config.py set {tempdir}/nosection.ini p 18 --no-section', 'b5a776c3d9888b6a05ed8fbfa14845c9')
run_test(f'../py-ini-config.py set {tempdir}/nosection.ini i 19 --view-comments --no-section -s " = "',
         '24cc445ff7e34de7134dcdecdcd787fa')
run_test(f'../py-ini-config.py set {tempdir}/nosection.ini i 8 --view-comments --no-section', '877fd863da1e631f703f8566f1b8c58d')
get_test(f'../py-ini-config.py get_commented {tempdir}/nosection.ini o --no-section', '9')
get_test(f'../py-ini-config.py get_commented {tempdir}/nosection.ini i --no-section', '19')
run_test(f'../py-ini-config.py comment {tempdir}/nosection.ini d --no-section', '0356818cd5cfad1978fed96c6628b6fc')
retcode_test(f'../py-ini-config.py is_set {tempdir}/nosection.ini --no-section d', 1)
retcode_test(f'../py-ini-config.py is_set {tempdir}/nosection.ini --no-section y', 0)
run_test(f'../py-ini-config.py uncomment {tempdir}/nosection.ini d --no-section', '877fd863da1e631f703f8566f1b8c58d')
run_test(f'../py-ini-config.py set {tempdir}/nosection.ini o 20 -s "     =" --no-section', 'da282bf8f803aeb267de6c2e7d4d0c03')
run_test(f'../py-ini-config.py comment {tempdir}/nosection.ini o --no-section', '13c542c078ce4653c9cb25b8c08a3f0b')
run_test(f'../py-ini-config.py set {tempdir}/nosection.ini o 21 --view-comments --no-section', 'a557107d5e50712128adbfdedb87265a')
run_test(f'../py-ini-config.py set {tempdir}/nosection.ini o 9 --view-comments --no-section', '1f8bf8f4dc79827f78733bd6c209dd80')

# working with several commented vars with one name
run_test(f'../py-ini-config.py del {tempdir}/section.ini d t', '7c1308af16ba50cda297bd1209be7b83')
run_test(f'../py-ini-config.py uncomment {tempdir}/section.ini d t', '5e5d48c443667433bfc5fefe19b88651')
run_test(f'../py-ini-config.py comment {tempdir}/section.ini d t', '7c1308af16ba50cda297bd1209be7b83')
run_test(f'../py-ini-config.py set {tempdir}/section.ini d t 84 --view-comments', '2870194f23cca9f2a4fba78d04b4c5ae')
run_test(f'../py-ini-config.py del {tempdir}/section.ini d t', '4e4763e11adfdd37333be6f75f737e19')

# working with several commented vars with one name with no section
run_test(f'../py-ini-config.py comment {tempdir}/nosection.ini o --no-section', '0b8456802ddc65af5b39fe6f2aa4142c')
run_test(f'../py-ini-config.py set {tempdir}/nosection.ini o 22 --no-section', 'debab7b1618f61f7e9fc0bc66576e824')
run_test(f'../py-ini-config.py comment {tempdir}/nosection.ini o --no-section', '4ecb066024b89615ab5a7701c301dc2f')
run_test(f'../py-ini-config.py del {tempdir}/nosection.ini o --no-section', '0b8456802ddc65af5b39fe6f2aa4142c')
run_test(f'../py-ini-config.py comment {tempdir}/nosection.ini p --no-section', '61ffe8ec55a41f122a36d9d920a51336')
run_test(f'../py-ini-config.py uncomment {tempdir}/nosection.ini p --no-section', '0b8456802ddc65af5b39fe6f2aa4142c')
run_test(f'../py-ini-config.py comment {tempdir}/nosection.ini p --no-section', '61ffe8ec55a41f122a36d9d920a51336')
run_test(f'../py-ini-config.py del {tempdir}/nosection.ini p --no-section', 'd14285539152ef51144713bd52ba167b')

# deleting empty and not empty sections
run_test(f'../py-ini-config.py del {tempdir}/section.ini a t --flush', '2ea86432eef4f9b8e52a58467a7c6f79')
run_test(f'../py-ini-config.py del {tempdir}/section.ini d t --flush', '2a49205632e29b7bb331a6e42b62abdf')
run_test(f'../py-ini-config.py del {tempdir}/section.ini d t', 'dda8c3c6823cbcd033db1b8f00be6e5f')
run_test(f'../py-ini-config.py del {tempdir}/section.ini d t --flush', 'bdbf48f842f14a62e97aed1afa1e1161')

print(tempdir)
print(f'Failed {fail_cnt} tests')
sys.exit(fail_cnt)
